package com.ljx.Exception;

import com.ljx.Domain.R;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@ResponseBody
@Log4j2
public class ControllerException {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> sqlExceptionHandler(SQLIntegrityConstraintViolationException exception){
        log.error(exception.getMessage());
        if (exception.getMessage().contains("Duplicate entry")){
            String message = exception.getMessage().split(" ")[2];
            return R.error(message+"已存在");
        }
        return R.error("未知错误");
    }
    @ExceptionHandler(AssociatedException.class)
    public R<String> associatedExceptionHandler(AssociatedException exception){
        log.error(exception.getMessage());
        return R.error(exception.getMessage());
    }
}
